/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CategoryForm.java
 *
 * Created on Apr 8, 2012, 8:56:15 PM
 */
package Admin;

import Constructor.Common;
import DAO.DAO_Category_Question;
import Model.Model_Category_Question;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 *
 * @author Admin
 */
public class CategoryForm extends javax.swing.JDialog {

    /** Creates new form CategoryForm */
    public CategoryForm(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        loadForm();
    }

    private void loadForm() {
        if (Common._category.equals("Update")) {
            setTitle("Update Category");
            lblTitle.setText("Update Category");
            butAddCategory.setText("Update");
            loadCategory();
        } else {
            setTitle("Add Category");
            lblTitle.setText("Add Category");
            butAddCategory.setText("Add");
        }

        this.setSize(330, 180);
        ImageIcon icon = new ImageIcon(getClass().getResource("/Images/logo.png"));
        Image image = icon.getImage();
        setIconImage(image);
        Dimension ScreenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation((ScreenSize.width - 330) / 2, (ScreenSize.height - 180) / 2);
        setResizable(false);
    }

    private void loadCategory() {
        DAO_Category_Question daoCategory = new DAO_Category_Question();
        Model_Category_Question modCategory = daoCategory.getOneCategoryByID(Common._CategoryID);
        txtCategoryName.setText(modCategory.getName());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtCategoryName = new javax.swing.JTextField();
        lblCategoryName = new javax.swing.JLabel();
        lblTitle = new javax.swing.JLabel();
        butExit = new javax.swing.JButton();
        butAddCategory = new javax.swing.JButton();
        lblError = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        lblCategoryName.setText("Category Name:");

        lblTitle.setFont(new java.awt.Font("Tahoma", 1, 18));
        lblTitle.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lblTitle.setText("Add Category");

        butExit.setText("Cancel");
        butExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                butExitActionPerformed(evt);
            }
        });

        butAddCategory.setText("Add");
        butAddCategory.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                butAddCategoryActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(54, 54, 54)
                        .addComponent(lblTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 212, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lblCategoryName, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(txtCategoryName, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(butAddCategory, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(butExit, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18))))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(lblError, javax.swing.GroupLayout.PREFERRED_SIZE, 281, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblTitle)
                .addGap(9, 9, 9)
                .addComponent(lblError)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblCategoryName)
                    .addComponent(txtCategoryName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(butExit)
                    .addComponent(butAddCategory))
                .addContainerGap(29, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void butExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butExitActionPerformed
        // TODO add your handling code here:
        if (JOptionPane.showConfirmDialog(this, "Do you readlly want to Exit?  ") == JOptionPane.YES_OPTION) {
            this.setVisible(false);
        }
    }//GEN-LAST:event_butExitActionPerformed

    private void butAddCategoryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_butAddCategoryActionPerformed
        // TODO add your handling code here:
        if (!txtCategoryName.getText().equals("")) {
            if (butAddCategory.getText().equals("Add")) {
                Model_Category_Question model = new Model_Category_Question();
                model.setName(txtCategoryName.getText());
                DAO_Category_Question daoCategoryQues = new DAO_Category_Question();
                boolean result = daoCategoryQues.Iinsert_Category(model);
                if (result == true) {
                    this.setVisible(false);
                } else {
                    lblError.setText("Error, insert failed!!! ");
                }
            } else {
                Model_Category_Question model = new Model_Category_Question();
                model.setId(Common._CategoryID);
                model.setName(txtCategoryName.getText());
                DAO_Category_Question daoCategoryQues = new DAO_Category_Question();
                boolean result = daoCategoryQues.Update_Category(model);
                if (result == true) {
                    this.setVisible(false);
                } else {
                    lblError.setText("Error, Update failed!!! ");
                }
            }
        } else {
            lblError.setText("Category name is not null!!! ");
        }
    }//GEN-LAST:event_butAddCategoryActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*java.awt.EventQueue.invokeLater(new Runnable() {
        
        public void run() {
        CategoryForm dialog = new CategoryForm(new javax.swing.JFrame(), true);
        dialog.addWindowListener(new java.awt.event.WindowAdapter() {
        
        public void windowClosing(java.awt.event.WindowEvent e) {
        System.exit(0);
        }
        });
        dialog.setVisible(true);
        }
        });*/
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton butAddCategory;
    private javax.swing.JButton butExit;
    private javax.swing.JLabel lblCategoryName;
    private javax.swing.JLabel lblError;
    private javax.swing.JLabel lblTitle;
    private javax.swing.JTextField txtCategoryName;
    // End of variables declaration//GEN-END:variables
}
